<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\home\controller;

use app\home\model\MemberModel;
use app\home\model\RechargeModel;
use cmf\controller\UserBaseController;
use think\Db;


class Member1Controller extends UserBaseController
{
    public function index(){

    return $this->fetch();
    }

    function memadd(){
        if($this->request->isPost()){
            $cc=$this->request->Post();
            $uid=cmf_get_current_user_id();
            $storeid=DB::name('user')->where('id',$uid)->column('storeid');           
            $cc['store_id']=$storeid[0];//店铺id            
            $mop=DB::name('member')->where('mobile',$cc['mop'])->field('mobile')->find();
            if(is_null($mop)){
                $cc['ctime']=explode(' ',$cc['ctime']);
                $cc['ctime']=join('',$cc['ctime']);
                $card_id=DB::name('member')->where('card_id',$cc['card_id'])->select();
                $idnumber=DB::name('member')->where('idnumber',$cc['idnumber'])->find();
                if($card_id){
                    if(!empty($cc['uname'])){

                        if(!empty($cc['mobile']) && preg_match("/^1[34578]{1}\d{9}$/",$cc['mobile'])){
                            if(!empty($cc['idnumber']) && is_null($idnumber)){
                                if(!empty($cc['ctime'])){
                                    if(!empty($cc['birthday'])){
                                        if(!empty($cc['address'])){
                                            $user=new MemberModel;
                                            if($cc['sex']='男'){
                                                $cc['sex']=1;
                                                $cc['jtime']=time();
                                                $user->save($cc);
                                                $this->success('添加成功');
                                            }elseif($cc['sex']='女'){
                                                $cc['sex']=2;
                                                $cc['jtime']=time();
                                                $cc['store_id']=111;
                                                $user->save($cc);
                                                $this->success('添加成功');
                                            }
                                        }else{
                                            $this->error('住址不能为空');
                                        }

                                    }else{
                                        $this->error('生日不能为空');
                                    }

                                }else{
                                    $this->error('办卡日期不能为空');
                                }

                            }else{
                                $this->error('证件编号为空,或者已有此会员证件编号');
                            }

                        }else{
                            $this->error('手机号格式不对');
                        }


                    }else{
                        $this->error('会员姓名不能为空');
                    }

                } else{
                    //echo "<script>alert('数据库已有此卡号')</script>";
                    $this->error('数据库已有此卡号');
                }
            }else{
              $mop=DB::name('member')->where('mobile',$cc['mop'])->field('mobile')->find();
                //var_dump($mop);exit;
                if(!is_null($mop)){
                    //echo "<script>alert('you此会员')</script>";

                   $select=DB::name('member')->where('mobile',$cc['mop'])->select();
                    //var_dump($select);exit;

                   /*$c= session('select',$select);
                    var_dump($c);exit;*/
                    $this->assign('select',$select);

                   return $this->fetch('member/select');

                }else{
                    echo "<script>alert('无此会员')</script>";
                }

            }
            //var_dump($cc);
        }

        return $this->fetch();
    }

    function select(){

       /* if($this->request->isPost()){
            $cc1=$this->request->Post();
           $where['card_id']=DB::name('member')->where('card_id',$cc1['mop'])->column('card_id');
            $where['mobile']=DB::name('member')->where('mobile',$cc1['mop'])->column('mobile');
            //var_dump($mobile);
            if($where){
                $cc=DB::name('member')->where('mobile',$cc1['mop'] )->select();
                var_dump($cc);
                //$this->assign('cc',$name);
                $this->assign('cc',$cc);
                return $this->fetch('member/memadd');
                //$this->success('进入查询页面');
            }

        }*/
      return $this->fetch();
    }
    function recharge(){
        if($this->request->isPost()){
            $cc=$this->request->Post();
            //var_dump($cc['mobile']);exit;
            if(!empty($cc['mobile'])){
                $member1=DB::name('member')->where('mobile',$cc['mobile'])->find();
               //var_dump($member);exit;
                //$member1=explode('',$member);
                if(is_null($member1)){

                    echo "<script>alert('没有此会员')</script>";
                }else{
                    $member=DB::name('member')->where('mobile',$cc['mobile'])->select();

                    $this->assign('member',$member);
                    return $this->fetch('member/findmem');
                }

            }else{
                echo "<script>alert('手机号不能为空')</script>";
            }



        }
        return $this->fetch('member/recharge');



    }

    function findmem(){

        return $this->fetch();
    }


    function addition(){
        if($this->request->isPost()){
            $data=$this->request->Post();
            $param = $this->request->param();
            $param=$param['id'];
            //var_dump($data);exit;

            if(!empty($data['money'])){
                if(!empty($data['sendmoney'])){
                    if(!empty($data['allmoney'])){
                        if(!empty($data['allbalance'])){
                            if(!empty($data['ctime'])){

                                $uid=cmf_get_current_user_id();
                                $storeid=DB::name('user')->where('id',$uid)->column('storeid');



                                $mem['storeid']=$storeid[0];//店铺id
                                //var_dump($mem['storeid']);exit;
                                $mem['operatorid']=cmf_get_current_user_id();;//操作员id,登录用户id
                                //var_dump($mem['operatorid']);exit;
                                $mem['card_id']=$data['card_id'];
                                $mem['userid']=$param;
                                $mem['money']=$data['money'];
                                $mem['sendmoney']=$data['sendmoney'];
                                $mem['allmoney']=$data['allmoney'];
                                $mem['allbalance']=$data['allbalance'];
                                $mem['ctime']=$data['ctime'];
                                $mem['way']=$data['way'];
                                //var_dump($mem);exit;

                               $user=new RechargeModel;
                                $user->save($mem);

                                $user1=new MemberModel;
                                $user1->where('id',$param)->setField('balance',$data['allbalance']);




                                $this->success('充值成功',url('recharge'));
                                //echo "<script>alert('充值成功')</script>";


                            }else{
                                //echo "<script>alert('创建时间不能为空')</script>";
                                $this->error('创建时间不能为空',url('recharge'));
                            }

                        }else{
                            //echo "<script>alert('总余额不能为空')</script>";
                            $this->error('总余额不能为空',url('recharge'));
                        }

                    }else{
                        //echo "<script>alert('总金额不能为空')</script>";
                        $this->error('总金额不能为空',url('recharge'));
                    }

                }else{
                   // echo "<script>alert('赠送金额不能为空')</script>";
                    $this->error('赠送金额不能为空',url('recharge'));
                }
            }else{
                //echo "<script>alert('充值金额不能为空')</script>";
                $this->error('充值金额不能为空',url('recharge'));
            }

        }

    }


}
























